package com.teacher.mobile.controller;


import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.teacher.common.CommonResult;
import com.teacher.pojo.dto.login.LoginDto;
import com.teacher.pojo.dto.teacher.TeacherAddDto;
import com.teacher.pojo.dto.teacher.TeacherGetByDeptDto;
import com.teacher.pojo.dto.teacher.TeacherUpdateDto;
import com.teacher.pojo.entity.PerTeacher;
import com.teacher.pojo.vo.teacher.LoginVo;
import com.teacher.pojo.vo.teacher.TeacherGetVo;
import com.teacher.pojo.vo.teacher.UserInfoVo;
import com.teacher.security.entity.annotation.WhiteApi;
import com.teacher.security.entity.permission.MenuReturnVo;
import com.teacher.service.IPerTeacherService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zfm
 * @since 2021-06-15
 */
@RestController
@RequestMapping("/pc/per-teacher")
public class PerTeacherController {

    @Autowired
    private IPerTeacherService iPerTeacherService;


    /**
     * 登录
     *
     * @param dto
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("登录")
    @WhiteApi
    public CommonResult<LoginVo> login(@RequestBody LoginDto dto) {
        return iPerTeacherService.login(dto);
    }
    /**
     * 修改用户角色
     * @param roleId
     * @return
     */
    @GetMapping("/exchangeRoles")
    @ApiOperation("修改用户角色")
    public CommonResult<String> exchangeRoles(Long roleId) {
        return iPerTeacherService.exchangeRoles(roleId);
    }
    /**
     * @return
     */
    @GetMapping("/menu")
    @ApiOperation("获取菜单")
    public CommonResult<Object> getMenu() {
        return iPerTeacherService.getMenu();
    }

    /**
     * logout
     *
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("用户登出")
    @WhiteApi
    public CommonResult<String> logoutUser() {
        return iPerTeacherService.logout();
    }

    /**
     * 获取用户信息
     *
     * @return
     */
    @GetMapping("/userInfo")
    @ApiOperation("获取用户信息")
    public CommonResult<UserInfoVo> getUserInfo() {
        return iPerTeacherService.getUserInfo();
    }

    @GetMapping("/topMenu")
    @ApiOperation("获取顶部菜单")
    public CommonResult getTopMenu() {
        String topUrl = "[{\n" +
                "  label: \"首页\",\n" +
                "  path: \"/wel/index\",\n" +
                "  icon: 'el-icon-document',\n" +
                "  meta: {\n" +
                "    i18n: 'dashboard',\n" +
                "  },\n" +
                "  parentId: 0\n" +
                "},\n" +
                "{\n" +
                "  label: \"测试\",\n" +
                "  icon: 'el-icon-document',\n" +
                "  path: \"/test\",\n" +
                "  meta: {\n" +
                "    i18n: 'test',\n" +
                "  },\n" +
                "  parentId: 1\n" +
                "},\n" +
                "{\n" +
                "  label: \"更多\",\n" +
                "  icon: 'el-icon-document',\n" +
                "  path: \"/wel/more\",\n" +
                "  meta: {\n" +
                "    menu: false,\n" +
                "    i18n: 'more',\n" +
                "  },\n" +
                "  parentId: 2\n" +
                "}]";

        return CommonResult.success(new JSONArray(topUrl));
    }

    /**
     * 根据部门查询教师用户
     *
     * @param dto
     * @return
     */
    @GetMapping("/department")
    @ApiOperation("根据部门查询教师用户")
    public CommonResult<IPage<TeacherGetVo>> getByDepartment(TeacherGetByDeptDto dto) {
        return iPerTeacherService.getByDepartmentId(dto);
    }

    /**
     * 更新信息
     *
     * @param dto
     * @return
     */
    @PutMapping("/user")
    @ApiOperation("更新信息")
    public CommonResult<String> updateUser(@RequestBody TeacherUpdateDto dto) {
        return iPerTeacherService.updateInfo(dto);
    }

    /**
     * 添加用户
     *
     * @param dto
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("添加用户")
    public CommonResult<String> add(@RequestBody TeacherAddDto dto) {
        return iPerTeacherService.add(dto);
    }

    /**
     * 删除用户
     *
     * @param teacherKey
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除用户")
    public CommonResult<String> del(@RequestBody List<String> teacherKey) {
        iPerTeacherService.remove(new QueryWrapper<PerTeacher>().lambda().in(PerTeacher::getTeacherKey,teacherKey));
        return CommonResult.success("删除成功");
    }

}

